package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbSpu;
import com.guigu.qingcheng.pojo.vo.Goods;
import com.guigu.qingcheng.pojo.vo.SpuAndSku;
import com.guigu.qingcheng.service.TbSpuService;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
*
* @author Jiang
* @date 2020/07/27
*/
@Slf4j
@RestController
@RequestMapping("/spu")
@Api(tags = "TbSpuController")
public class TbSpuController {
    @Resource
    private TbSpuService tbSpuService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") String id) {

        TbSpu tbSpu = tbSpuService.findById(id);
        return R.ok().data("tbSpu",tbSpu);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbSpu>  list = tbSpuService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbSpu> pageUtils = tbSpuService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbSpu tbSpu, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbSpu> pageUtils = tbSpuService.pageQueryByAll(pageNum, pageSize, tbSpu);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbSpu tbSpu) {

        boolean b = tbSpuService.saveTbSpu(tbSpu);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbSpu tbSpu) {
        boolean b = tbSpuService.updateTbSpu(tbSpu);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        boolean b = tbSpuService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "添加商品信息")
    @PostMapping("/save")
    public R save(@RequestBody Goods goods){
        boolean b = tbSpuService.saveGoods(goods);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "逻辑删除商品信息")
    @DeleteMapping("/isDeleteById/{id}")
    public R save(@PathVariable("id") String id){
        boolean b = tbSpuService.isDeleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/findGoodsById/{id}")
    public R findGoodsById(@PathVariable String id){
        Goods goods = tbSpuService.findGoodsById(id);
        return R.ok().message("查询成功").data("goods", goods);
    }

    @GetMapping("/findGoodById")
    public String findGoodById(String id){
        Goods goods = tbSpuService.findGoodsById(id);
        String value = String.valueOf(goods);
        return value;
    }

    /**
     * 商品审核 通过自动上架
     * @param id
     * @param status
     * @param message
     * @return
     */
    @GetMapping("/audit/{id}/{status}/{message}")
    public R audit(@PathVariable("id") String id,@PathVariable("status") String status,@PathVariable("message") String message){
        tbSpuService.audit(id, status, message);
        return R.ok().message("修改成功");
    }

    /**
     * 下架商品
     * @param id
     * @return
     */
    @GetMapping("/pull")
    public R pull(String id){
        tbSpuService.pull(id);
        return R.ok().message("下架成功");
    }

    /**
     * 上架商品
     * @param id
     * @return
     */
    @GetMapping("/put")
    public R put(String id){
        tbSpuService.put(id);
        return R.ok().message("上架成功");
    }

    /**
     * 批量上架商品
     * @param ids
     * @return
     */
    @GetMapping("/putMany")
    public R putMany(String[] ids){
        Integer i = tbSpuService.putMany(ids);
        return R.ok().message("上架了" + i + "个商品");
    }

    @ApiOperation(value = "通过条件查询出所有未审核的商品并分页")
    @PostMapping("/selectSpuByStatus/{pageNum}/{pageSize}")
    public R selectSpuByStatus(@RequestBody TbSpu tbSpu, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbSpu> pageUtils = tbSpuService.selectSpuByStatus(pageNum, pageSize, tbSpu);
        return R.ok().data("pageUtils", pageUtils);
    }

    @ApiOperation(value = "查询spu和sku链表 未删除的")
    @PostMapping("selectSpuAndSkuIsDelete/{pageNum}/{pageSize}")
    public R selectSpuAndSkuIsDelete(@RequestBody TbSpu tbSpu, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<SpuAndSku> pageUtils = tbSpuService.selectSpuAndSkuIsDelete(pageNum, pageSize, tbSpu);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "查询spu和sku链表 未删除的 并且未审核的")
    @PostMapping("selectSpuAndSkuOnStatus/{pageNum}/{pageSize}")
    public R selectSpuAndSkuOnStatus(@RequestBody TbSpu tbSpu, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<SpuAndSku> pageUtils = tbSpuService.selectSpuAndSkuOnStatus(pageNum, pageSize, tbSpu);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "查询spu和sku链表 已逻辑删除的")
    @PostMapping("selectSpuAndSkuDelete/{pageNum}/{pageSize}")
    public R selectSpuAndSkuDelete(@RequestBody TbSpu tbSpu, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<SpuAndSku> pageUtils = tbSpuService.selectSpuAndSkuDelete(pageNum, pageSize, tbSpu);
        return R.ok().data("pageUtils",pageUtils);
    }
}
